package ssm.Tools;

import java.util.List;

/**
 * 分页查询，相关属性封装
 * 该实体类可直接转化JSON串   JSONObject.put("page",Page<T>)
 * @param <T>
 */
public class Page<T> {
    //范型 ，对象集合
    private List<T> list;
    //当前页
    private Integer PageNum;
    //上一页
    private Integer prePage;
    //下一页
    private Integer nextPage;
    //总页数
    private Integer TotalPage;
    //每页显示数据数
    private Integer PageSize;
    //sql起始索引
    private Integer index;
    //总记录条数
    private Integer TotalCount;

    public Page(Integer pageNum, Integer pageSize, Integer totalCount) {
        this.PageNum = pageNum;
        this.PageSize = pageSize;
        this.TotalCount = totalCount;
        //通过（总数据数/每页展示数）来获得总页数
        if(totalCount%pageSize==0){
            this.TotalPage=totalCount/pageSize;
        }else{
            this.TotalPage=totalCount/pageSize+1;
        }
        //用当前页来确定上一页的页码
        if(pageNum>1){
            this.prePage=pageNum-1;
        }else{
            this.prePage=1;
        }
        //用当前页来确定尾页的页码
        if(pageNum<TotalPage){
            this.nextPage=pageNum+1;
        }else{
            this.nextPage=TotalPage;
        }
        //获得sql中当前页起始目标索引
        this.index=pageSize*(pageNum-1);
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Integer getPageNum() {
        return PageNum;
    }

    public void setPageNum(Integer pageNum) {
        PageNum = pageNum;
    }

    public Integer getPrePage() {
        return prePage;
    }

    public void setPrePage(Integer prePage) {
        this.prePage = prePage;
    }

    public Integer getNextPage() {
        return nextPage;
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public Integer getTotalPage() {
        return TotalPage;
    }

    public void setTotalPage(Integer totalPage) {
        TotalPage = totalPage;
    }

    public Integer getPageSize() {
        return PageSize;
    }

    public void setPageSize(Integer pageSize) {
        PageSize = pageSize;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public Integer getTotalCount() {
        return TotalCount;
    }

    public void setTotalCount(Integer totalCount) {
        TotalCount = totalCount;
    }
}
